package org.springcloud.wisdom.spring_02_classroom.student.sschedule.service;

import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.entity.Course;
import org.springcloud.wisdom.spring_02_classroom.bean.Page;
import org.springcloud.wisdom.spring_02_classroom.student.sschedule.entity.Student;
import org.springcloud.wisdom.spring_02_classroom.student.sschedule.dao.StudentScheduleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentScheduleService {
    @Autowired
    private StudentScheduleDao studentScheduleDao;

    //学生查看自己名下的课程
    public List<Course> findCoursesByTid(String sid) {
        if (sid == null || sid.trim().length() == 0){
            return null;
        }
        return studentScheduleDao.selectCoursesByTid(sid);
    }

    //学生查看所属课程的同学名单
    public List<Student> getStudentsByTid(String cid) {
        if (cid == null || cid.trim().length() == 0){
            return null;
        }
        return studentScheduleDao.selectCourseStudent(cid);
    }

    //课程历史
    public Page<Course> getcoursehistory(int pageNum, int pageSize, String sid  ){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Course> list= studentScheduleDao.getcoursehistory(start,pageSize,sid);
        //获取总条目数
        int total= studentScheduleDao.Countcoursehistory(sid);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Course> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
}